package com.example.test0702;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.test0702.adapter.PhoneAdapter;
import com.example.test0702.yan.Phone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    String TAG="MainActivity";
    private RecyclerView phonesRc;
    private  PhoneAdapter adapter;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<Phone> phones=new ArrayList<>();
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url("https://api.myjson.com/bins/lw4xn")
                .get()
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }


            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json=response.body().string();
                try {
                    JSONArray ary=new JSONArray(json);
                    for (int i = 0; i < ary.length(); i++) {
                        JSONObject obj=ary.getJSONObject(i);
                        Phone phone=new Phone();
                        phone.setName(obj.getString("name"));
                        phone.setPrice((float)obj.getDouble("price"));
                        phone.setImg(obj.getString("img"));

                        phones.add(phone);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                            }
                        });
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        phonesRc=findViewById(R.id.phones_rc);

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        phonesRc.setLayoutManager(manager);
        adapter=new PhoneAdapter(phones,this);

        phonesRc.setAdapter(adapter);
    }

    public void net(View view) {
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                //.url("http://www.baiud.com")
                .url("https://api.myjson.com/bins/17cpcv")
                .get()
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String rs=response.body().string();
                try {
                    JSONArray ary=new JSONArray(rs);
                    for (int i = 0; i < ary.length(); i++) {
                        String name=ary.getJSONObject(i).getString("name");
                        float price=(float) ary.getJSONObject(i).getDouble("price");
                        Phone phone=new Phone();
                        phone.setName(name);
                        phone.setPrice(price);
                        Log.i(TAG, "onResponse: "+name+"\t"+price);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
